/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.coraci.anag.test;

import it.coraci.anag.db.mongo.entities.MongoDocument;
import it.coraci.anag.db.sql.entities.IDDocument;
import it.coraci.anag.db.sql.entities.Person;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDCentral;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDUpdateEntityListener;
import java.beans.Beans;

/**
 *
 * @author Luca
 */
public class GeneralTesterFrame extends javax.swing.JFrame implements CRUDUpdateEntityListener{

    /**
     * Creates new form GeneralTesterFrame
     */
    public GeneralTesterFrame() {
        initComponents();
        this.genericOverviewPanel1.prepareForClass(Person.class);
        this.cRUDPanel_Documents.prepareForClass(MongoDocument.class);
        this.cRUDPanel_Documents.setNoSQL(true);
        if(!Beans.isDesignTime()){
            CRUDCentral.getInstance().addCRUDUpdateEntityListener(this);
            CRUDCentral.getInstance().init();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        peopleManagerPanel1 = new it.coraci.anag.gui.people.PeopleManagerPanel();
        docTypeManagerPanel1 = new it.coraci.anag.gui.docs.DocTypeManagerPanel();
        documentManagerPanel1 = new it.coraci.anag.gui.docs.DocumentManagerPanel();
        peopleOverview21 = new it.coraci.anag.gui.people.PeopleOverview2();
        jPanel1 = new javax.swing.JPanel();
        genericOverviewPanel1 = new it.coraci.anag.gui.components.basic.gui.generic.GenericOverviewPanel();
        cRUDPanel_Documents = new it.coraci.anag.gui.components.basic.gui.CRUDPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTabbedPane1.addTab("Persone", peopleManagerPanel1);
        jTabbedPane1.addTab("Tipologie Documentali", docTypeManagerPanel1);
        jTabbedPane1.addTab("Gestione Documentale", documentManagerPanel1);
        jTabbedPane1.addTab("tab4", peopleOverview21);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(genericOverviewPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRUDPanel_Documents, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(genericOverviewPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
            .addComponent(cRUDPanel_Documents, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("MAIN TESTER PANEL", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 478, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GeneralTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GeneralTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GeneralTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GeneralTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GeneralTesterFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private it.coraci.anag.gui.components.basic.gui.CRUDPanel cRUDPanel_Documents;
    private it.coraci.anag.gui.docs.DocTypeManagerPanel docTypeManagerPanel1;
    private it.coraci.anag.gui.docs.DocumentManagerPanel documentManagerPanel1;
    private it.coraci.anag.gui.components.basic.gui.generic.GenericOverviewPanel genericOverviewPanel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private it.coraci.anag.gui.people.PeopleManagerPanel peopleManagerPanel1;
    private it.coraci.anag.gui.people.PeopleOverview2 peopleOverview21;
    // End of variables declaration//GEN-END:variables

    @Override
    public void updateThisEntity(Class<?> entityClazz, Object id) {
        
    }

    @Override
    public void entitySelected(Object entity) {
        if(entity instanceof Person) {
            if (((Person) entity).getIddocuments().isEmpty()) {
                this.cRUDPanel_Documents.clear();
            }
        }
        if (entity instanceof IDDocument) {
            String fileID = ((IDDocument) entity).getFileID();
            if(fileID == null || fileID.isEmpty()){
                System.out.println("no file id has been found");
                return;
            }
            MongoDocument findMongoDocument = (MongoDocument)CRUDCentral.getInstance().findEntity(MongoDocument.class, fileID);
            if(findMongoDocument!=null){
            this.cRUDPanel_Documents.loadEntity(findMongoDocument);
            }else{
                System.out.println("no documents has been found !");
            }
        }
    }

    @Override
    public void newEntity(Object entityClazz) {
        System.out.println(":D");
    }
}
